#!/usr/bin/env python3
# -*- coding: utf-8 -*-

__autho__ = "Tony.Don"
__lastupdatetime__ = "2017/09/20"
import os
import threading
import re
import application.webanalysis
import time,datetime
from core.Spider import myhttp
from core.parser import parserHtml
from core.rule import rule
from os.path import os
from models.domain import domain

'''
class\r\n
    rule.gzw.gzw(rule)\r\n
description\r\n
    分析规则(html分析法)\r\n
'''
class gzw(rule):

    __filename__=None
    __tpath__=None
    def __init__(self,domain):
        self._domain=domain
        '''Create location file,delete
        self.__tpath__=os.path.join(os.path.abspath("."),"html")
        if not os.path.exists(self.__tpath__):
            os.mkdir(self.__tpath__)
        self.__tpath__=os.path.join(self.__tpath__,time.strftime("%Y",time.localtime()))
        if not os.path.exists(self.__tpath__):
            os.mkdir(self.__tpath__)
        self.__tpath__=os.path.join(self.__tpath__,time.strftime("%m",time.localtime()))
        if not os.path.exists(self.__tpath__):
            os.mkdir(self.__tpath__)
        self.__tpath__=os.path.join(self.__tpath__,time.strftime("%d",time.localtime()))
        if not os.path.exists(self.__tpath__):
            os.mkdir(self.__tpath__)
        '''
    def getRule(self,content):
        analysis=application.webanalysis.analysisHtml()
        analysis.addlisten(analysis.__event__.e_tag,self.callback)
        parserHtml(analysis).feed(content)

    def callback(self,args):
        htmlContent=None
        if args.__decl__:
            if htmlContent:
                htmlContent="<"+args.__decl__+">"
            else:
                htmlContent="<"+args.__decl__+">"+htmlContent
        if args.__s_tag__:
            if args.__s_tag__=="body":
                pass
            htmlContent="<"+args.__s_tag__
            for attr in args.__attrs__:
                htmlContent+=" {0}=\"{1}\"".format(attr[0],attr[1])
                if attr[0].lower()=="href":
                    re_domain=re.compile('^((http|https)://|//)(www.)?(\w+(\.)?)+',re.I) #匹配URL
                    domainName=re_domain.match(attr[1])
                    if domainName:
                        result=domain()
                        result.domainName=domainName.group()
                        result.lastGetTime=datetime.datetime.utcnow()
                        result.hashMD5=domainName.group()
                        if not result.findOne({'name':result.domainName}):
                            result.toSave()
                            print("save domain %s" % (domainName.group()))
                    else:
                        #非域名
                        '''
                        reg_rule=re.compile(r'^//(\w+(\.)?)+',re.I)
                        domainName=reg_rule.search(attr[1])
                        if domainName:
                            result=domain()
                            result.domainName=domainName.group()
                            result.lastGetTime=time.localtime()
                            if not result.findOne({'name':result.domainName}):
                                result.toSave()
                                print("save domain %s" % (domainName[0]))
                        else:
                            if attr[1].find("/")==0:
                                domainName="%s%s" % (self._domain,attr[1])
                                result=domain()
                                result.domainName=domainName
                                result.lastGetTime=time.localtime()
                                if not result.findOne({'name':domainName}):
                                    result.toSave()
                                    print("save domain %s" % (domainName))
                            '''
            htmlContent+=">"
        if args.__data__:
            htmlContent+=args.__data__
        if args.__e_tag__:
            htmlContent+="</"+args.__e_tag__+">"       
        
        '''Save location file,Delete
        if(args.__s_tag__=="title"):
            self.__filename__= args.__data__+".html"
        if self.__filename__:
            if htmlContent:
                htmlContent.replace(u'\xa0', u' ')
                savePath=os.path.join(self.__tpath__,self.__filename__)
                if not os.path.exists(savePath):
                    fs=open(savePath,mode='a')
                    fs.writelines(htmlContent)
                    fs.flush()
                    fs.close()                 
                else:
                    pass
        '''